package com.weimob.mcloud.wechatwork.customer.service.user.utils;

import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSON;
import com.weimob.dubbo.generic.bean.response.ErrorCode;
import com.weimob.dubbo.generic.utils.threadlocal.SoaContext;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BaseErrorCode;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.zipkin.ZipkinContext;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wufeng on 2016/9/7.
 * 判断dubbo 业务调用是否正常
 */
@Slf4j
public class SoaResponseUtil {

    public static boolean isSuccNotNull(SoaResponse soaResponse) {
        return soaResponse != null && soaResponse.getResponseVo() != null && isSuccess(soaResponse.getReturnCode());
    }

    public static boolean isOk(SoaResponse soaResponse) {
        return soaResponse != null && isSuccess(soaResponse.getReturnCode());
    }

    public static <E> E unpack(SoaResponse<E, ?> soaResponse) {
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("soaResponse error, ticket:{}, response:{}, ",
                ZipkinContext.getContext().getGlobalTicket(),JSON.toJSONString(soaResponse));
            //错误码转换
            if (100300600001L == Long.parseLong(soaResponse.getReturnCode())) {
                throw new BizException(BaseErrorCode.FAILED_TO_GET_AUTHORIZER_ACCESS_TOKEN);
            }
            else if (100300600002L == Long.parseLong(soaResponse.getReturnCode())) {
                throw new BizException(BaseErrorCode.FAILED_TO_GET_AUTHORIZER_ACCESS_TOKEN);
            }
            else if (100300600192L == Long.parseLong(soaResponse.getReturnCode())) {
                throw new BizException(BaseErrorCode.NO_AUTHORITY_OF_EXTERNAL_USER);
            }


            throw new BizException(Long.parseLong(soaResponse.getReturnCode()),soaResponse.getReturnMsg());
        }

        return soaResponse.getResponseVo();
    }

    public static <E> SoaResponse<E, Void> ok(E responseVo) {
        SoaResponse<E, Void> soaResponse = new SoaResponse<>();
        soaResponse.setResponseVo(responseVo);
        soaResponse.setReturnCode(ErrorCode.SUCCESS.getErrorCode());
        soaResponse.setReturnMsg(ErrorCode.SUCCESS.getErrorMsg());
        SoaContext.getLocal().clear();
        SoaContext.getGlobal().clear();
        return soaResponse;
    }

    public static <E> E unpackNotNull(SoaResponse<E, ?> soaResponse) {
        if (!isSuccess(soaResponse.getReturnCode())) {
            throw new RpcException(soaResponse.getReturnMsg() + "_globalTicket:" + soaResponse.getGlobalTicket());
        }
        if (null == soaResponse.getResponseVo()) {
            throw new RpcException("RPC return entity is null" + soaResponse.getClass().getTypeParameters()[0].getClass().getSimpleName());
        }
        return soaResponse.getResponseVo();
    }


    private static boolean isSuccess(String code) {
        return 0L == Long.valueOf(code);
    }

}
